<template>
  <div>
    <el-table style="width: 100%;" border :data="records">
      <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
      <el-table-column label="名称" prop="skuName"></el-table-column>
      <el-table-column label="描述" prop="skuDesc"></el-table-column>
      <el-table-column label="默认图片" width="110">
        <template slot-scope="{row, $index}">
          <img :src="row.skuDefaultImg" alt="" style="width: 80px;height: 80px;">
        </template>
      </el-table-column>
      <el-table-column label="重量(KG)" prop="weight" width="80"></el-table-column>
      <el-table-column label="价格(元)" prop="price" width="80"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{row, $index}">
          <el-button type="info" icon="el-icon-bottom" size="mini" v-if="row.isSale == 0"
            @click="onSale(row)"></el-button>
          <el-button @click="cancelSale(row)" type="success" icon="el-icon-top" size="mini" v-else></el-button>
          <el-button @click="edit" type="primary" icon="el-icon-edit" size="mini"></el-button>
          <el-button @click="getSkuInfo(row)" type="info" icon="el-icon-info" size="mini"></el-button>
          <el-button @click="deleteSku(row)" type="danger" icon="el-icon-delete" size="mini"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
      :page-sizes="[3, 5, 10]" :page-size="limit" layout="prev, pager, next, jumper, ->, sizes, total" :total="total"
      style="text-align: center;">
    </el-pagination>
    <el-drawer :visible.sync="drawer" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag type="success" v-for="(attr, index) in skuInfo.skuAttrValueList" :key="attr.id">{{ attr.attrId }}-{{
              attr.valueId }}</el-tag>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel indicator-position="outside">
            skuImageList
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" alt="">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>

</template>

<script>

export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 10,
      total: 1,
      records: [],//sku列表数据
      skuInfo: {},//存储sku信息
      drawer: false,
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    async getSkuList() {
      const { page, limit } = this
      let result = await this.$API.sku.reqSkuList(page, limit)
      if (result.code == 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    handleCurrentChange(page) {
      this.page = page
      this.getSkuList()
    },
    handleSizeChange(limit) {
      this.limit = limit
      this.getSkuList()
    },
    async onSale(row) {
      let result = await this.$API.sku.reqOnSale(row.id)
      if (result.code == 200) {
        row.isSale = 1
        this.$message({ type: 'success', message: '上架成功' })
      }
    },
    async cancelSale(row) {
      let result = await this.$API.sku.reqCancelSale(row.id)
      if (result.code == 200) {
        row.isSale = 0
        this.$message({ type: 'success', message: '下架成功' })
      }
    },
    edit() {
      this.$message("正在开发中")
    },
    //
    async getSkuInfo(sku) {
      this.drawer = true
      let result = await this.$API.sku.reqSkuById(sku.id)
      if (result.code) {
        this.skuInfo = result.data
      }
    },
    //
    async deleteSku(sku) {
      let result = await this.$API.sku.reqDeleteSkuById(sku.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功' })
        if (this.records > 1) {
          this.page -= 1

        }
        this.getSkuList() 
      }
    }
  }
}
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}

.el-carousel__button {
  width: 10px;
  height: 10px;
  background-color: red;
  border-radius: 50%;
}
</style>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}

.el-col {
  margin: 10px;
}
</style>